import {
    editView
} from './views/edit.view';
import {
    validate
} from './validate';
import {
    getCateList
} from './models/getCatelist.model';
import {
    submit
} from './submit';
import {
    msg, modal6
} from '../common/layer';
import {
    upFile
} from './operate';
import {
    uploadImg
} from '../common/uploadImg';
const editBtnHandler = () => {

    let selectedList = $('#table').bootstrapTable('getSelections');
    if (selectedList.length === 0) {
        msg('请选择一项编辑');
        return;
    }

    const localData = selectedList[0];
    if(localData.is_url == 1){
        localData.link=localData.content_zh;
    }

    modal6('编辑', editView(localData));

    $(".modal-form input").iCheck({
        checkboxClass: 'icheckbox_minimal-blue',
        radioClass: 'iradio_minimal-blue'
    });
    $('#yes').on('ifChecked', function (e) {
        $(".contentShow").hide();
        $(".uploadContent").show();
    });
    $('#no').on('ifChecked', function (e) {
        $(".uploadContent").hide();
        $(".contentShow").show();
    });
    if(localData.is_url == 1){
        $(".contentShow").hide();
        $(".uploadContent").show();

    }
    window.upFile=upFile;

    uploadImg('#picker2', '#thumb'); // 缩略图

    $("#picker2").siblings('.list').html('<div class="file-item thumbnail" data-tips="1">' + '<img src="' + localData.thumb + '" class="editImg">' + '</div>');
    $("#picker2").siblings('#thumb').val(localData.thumb);
    $('#sort').val(localData.sort);


    $(document).on('click','#tabbutton li',function(){
        let index = $(this).index();
        if(index == 1){
            $('.baseInfo').removeClass('active').addClass('shownone');
            $('.detail').removeClass('shownone').addClass('active');
            $(this).addClass('hover').siblings('li').removeClass('hover');
        }else{
            $('.detail').removeClass('active').addClass('shownone');
            $('.baseInfo').removeClass('shownone').addClass('active');
            $(this).addClass('hover').siblings('li').removeClass('hover');
        }
    });

    getCateList(localData.cat_id);
    $("#myEditor1").html(localData.content_zh);
    $("#umeditor_textarea_content_zh").val(localData.content_zh);
    $("#myEditor2").html(localData.content_en);
    $("#umeditor_textarea_content_en").val(localData.content_en);

    $('#submitBtn').click(() => {
        let data = {};
        $.each($('#form').serializeArray(), (key, val) => {
            data[val.name] = val.value;
        });
        if (!validate(data)) {
            return;
        }
        data.id = localData.id;
        submit(GLOBAL_articleEditUrl, data);
    });


};


export {
    editBtnHandler
}